<template>
  <nav-bar />

  <!-- 导航按钮 -->

  <van-grid :column-num="5" :icon-size="50">
    <template v-for="(item, index) in Nav" :key="index">
      <van-grid-item :icon="item.image" :text="item.title" @click="gotoSearchList(item.title)" />
    </template>
  </van-grid>

  <!-- 瀑布列表 -->

  <van-list
    v-model:loading="loading"
    :finished="finished"
    finished-text="没有更多了"
    :immediate-check="false"
    @load="onLoad"
    class="goods-list"
  >
    <ul class="two-column-layout goods">
      <li
        v-for="(item, index) in goods"
        :key="index"
        class="goods-item"
        @click="goToClip(item.itemIdUrl, item.actorIdUrl)"
      >
        <div class="cover">
          <img :src="item.itemImage" :alt="item.title" class="goods-image" />

          <img
            :src="item.lefttop_taglist[0].img"
            class="lefttop-img"
            v-if="item.lefttop_taglist[0]"
          />

          <van-icon name="play-circle" class="play-icon" size="24px" />

          <div class="avatar-name">
            <img :src="item.actorAvatar" alt="" class="avatar" />

            <span class="name">{{ item.actorName }}</span>
          </div>
        </div>

        <span class="goods-title">{{ item.title }} </span>

        <div class="goods-status">
          <span class="price">{{ item.discountPrice }}</span>
          <img :src="item.bottomIcon" class="bottom-icon" />
          <span class="sale">{{ item.sale }}</span>
        </div>
      </li>
    </ul>
  </van-list>

  <tab-bar />
</template>

<script setup>
import { getHomeNav, getHomeLiveList } from '@/api/index.js'
import { ref, onMounted } from 'vue'
import NavBar from '@/components/common/NavBar.vue'

import { useRouter } from 'vue-router'
const router = useRouter()

const Nav = ref([])

const loading = ref(false)
const finished = ref(false)

const goods = ref([])
const page = ref(1)
const isEnd = ref(false)

onMounted(async () => {
  // 加载首页导航
  const menu = await getHomeNav()
  Nav.value = menu

  loading.value = true

  // 加载首页商品列表 第一页数据
  const res = await getHomeLiveList(page.value)
  // console.log("goods",goods);
  goods.value = res.data.list
  isEnd.value = res.data.isEnd
  loading.value = false
})

// 加载更多
const onLoad = async () => {
  if (isEnd.value) {
    finished.value = true
    return
  }
  page.value += 1
  loading.value = true
  const res = await getHomeLiveList(page.value)
  goods.value.push(...res.data.list)
  isEnd.value = res.data.isEnd
  loading.value = false
}

// 直播回放
const goToClip = (itemId, actorId) => {
  router.push({
    path: '/clip',
    query: {
      itemId,
      actorId,
    },
  })
}

const gotoSearchList = (keyword) => {
  router.push({ path: '/searchList', query: { q: keyword } })
}
</script>

<style lang="scss" scoped>
.goods-list {
  width: 100vw;
  overflow: hidden;
  box-sizing: border-box;
  padding: 0 10px;

  .goods {
    list-style: none;
    column-count: 2; // 改用多列布局
    column-gap: 10px; // 列间
  }

  .goods-item {
    background-color: #fff;
    border-radius: 5px;
    margin-bottom: 10px; // 统一底部间距
    break-inside: avoid; // 防止内容被分割
    display: inline-block; // 多列布局必要属性
    width: 100%; // 占满列宽

    .cover {
      flex-shrink: 0;
      position: relative;
      width: 100%;
      box-sizing: border-box;
      aspect-ratio: 1/1;
      overflow: hidden;

      .goods-image {
        position: relative;
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-top-left-radius: 5px;
        border-top-right-radius: 5px;
      }

      .lefttop-img {
        position: absolute;
        top: 10px;
        left: 0;
        width: 50px;
      }

      .play-icon {
        position: absolute;
        top: 10px;
        right: 10px;
      }

      .avatar-name {
        position: absolute;
        bottom: 10px;
        left: 10px;
        display: flex;
        align-items: center;
        justify-content: flex-start;

        .avatar {
          width: 20px;
          height: 20px;
          border-radius: 50%;
        }

        .name {
          color: #fff;
          font-size: 12px;
        }
      }
    }

    .goods-title {
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      font-size: 14px;
      line-height: 20px;
      color: #333;
      margin: 10px 0;
      padding: 0 10px;
    }

    .goods-status {
      display: flex;
      gap: 10px;
      justify-content: flex-start;
      align-items: center;
      font-size: 14px;
      padding: 0 10px 10px;
      /* margin: 10px 0; */
      color: #f46;

      .price {
      }

      .bottom-icon {
        height: 12px;
      }

      .sale {
        margin-left: auto;
        font-size: 12px;
        background: #ffecef;
        padding: 2px 6px;
        border-radius: 10px;
      }
    }
  }
}
</style>
